//
//  CZStatusViewModel.swift
//  weibo10
//
//  Created by apple on 16/5/18.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class CZStatusViewModel: NSObject
{
    //单例
    static let sharedInstance: CZStatusViewModel = CZStatusViewModel()
    
//私有构造函数
    private override init() {
        super.init()
    }
    
    //加载微博数据
    func loadStatus(loadStatusCallback:(statuses: [CZStatus]?, error: NSError?) ->())
    {
    
      guard let access_token = CZUserAccountViewModel.sharedInstance.userAccount?.access_token else
      {
        CZPrint(items: "access_token没有值，不去请求微博数据")
        return
        
        }
    
        //获取路径
        let urlString = "2/statuses/home_timeline.json"
        
        
        //参数
        let parameters = ["access_token": access_token]
        
        
        //发送请求
        CZNetworkTool.sharedInstance.request(RequestMethod.GET, URLString: urlString, parameters: parameters, success: { (_, responseObject) -> Void in
           
            if let result = responseObject as? [String: AnyObject]
            {
            //获取到所有的微博数据
                if let statuses = result["statuses"] as? [[String: AnyObject]]
                {
                //定义空数组存放转好的微博模型
                    var statusArray = [CZStatus]()
                    //遍历数组，取出每一个微博字典
                    for dict in statuses
                    {
                   //将字典转模型
                    let statusModel = CZStatus(dict: dict)
                        
                  //将模型添加到数组中
                        statusArray.append(statusModel)
                        }
                    CZPrint(items: "\(statusArray)")
                   //将微博数据告诉homeViewController
                    loadStatusCallback(statuses: statusArray, error: nil)
                        
                    }
            }
            })
            {(_,error) -> Void in
                loadStatusCallback(statuses: nil, error: error)
        }
    }
}
            
            